﻿@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<MDCard>
    <DataTableCard TModel="EntryOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" Title="@($"{_name}列表")" ShowAddButton="false" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <div style="width: 170px;">
                    <MIcon Color="info"
                           Class="mr-2"
                           OnClick="()=>EditItemPriority(context.Item,true)">@IconType.Up.ToIconString()</MIcon>
                    <MIcon Color="info"
                           Class="mr-2"
                           OnClick="()=>EditItemPriority(context.Item,false)">@IconType.Down.ToIconString()</MIcon>
                    @if (context.Item.IsHideOutlink)
                    {
                        <MIcon Color="success"
                               Class="mr-2"
                               OnClick="()=>HideItemOutlink(context.Item,false)">@IconType.Link.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>HideItemOutlink(context.Item,true)">@IconType.UnLink.ToIconString()</MIcon>
                    }
                    @if (context.Item.CanComment)
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>EditItemCanComment(context.Item,false)">@IconType.DisableComment.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="success"
                               Class="mr-2"
                               OnClick="()=>EditItemCanComment(context.Item,true)">@IconType.Comment.ToIconString()</MIcon>
                    }
                    @if (context.Item.IsHidden)
                    {
                        <MIcon Color="success"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,false)">@IconType.Check.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,true)">@IconType.Close.ToIconString()</MIcon>
                    }

                </div>
            }
            else if (context.Header.Value == nameof(EntryOverviewModel.IsHidden))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Recommends.StateChip IsHidden="context.Item.IsHidden" />
            }
            else if (context.Header.Value == nameof(EntryOverviewModel.Name))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Users.Certifications.EntryChip Id="@context.Item.Id" Name="@context.Item.Name" Type="context.Item.Type" />
            }
            else if (context.Header.Value == nameof(EntryOverviewModel.IsHideOutlink))
            {
                <OutlinkChip IsHideOutlink="context.Item.IsHideOutlink" />
            }
            else if (context.Header.Value == nameof(EntryOverviewModel.CanComment))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Users.CanCommentChip CanComment="context.Item.CanComment" />
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>

@code {
    private string _baseUrl = "api/entries/List";
    private string _name = "词条";

    private List<DataTableHeader<EntryOverviewModel>> _headers = new List<DataTableHeader<EntryOverviewModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(EntryOverviewModel.Id)
          },
          new (){ Text= "名称", Value= nameof(EntryOverviewModel.Name)},
          new (){ Text= "优先级", Value= nameof(EntryOverviewModel.Priority)},
          new (){ Text= "状态", Value= nameof(EntryOverviewModel.IsHidden)},
          new (){ Text= "外部链接", Value= nameof(EntryOverviewModel.IsHideOutlink)},
          new (){ Text= "留言板", Value= nameof(EntryOverviewModel.CanComment)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<EntryOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    public async Task EditItemPriority(EntryOverviewModel model, bool up)
    {
        string operationName = up ? "增权重" : "降权重";
        try
        {
            var obj = await _httpService.PostAsync<EditEntryPriorityViewModel, Result>("api/entries/EditEntryPriority", new EditEntryPriorityViewModel { Ids = new int[]{model.Id}, PlusPriority = up?1:-1 });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }

    public async Task HideItem(EntryOverviewModel model, bool isHidden)
    {
        string operationName = isHidden ? "隐藏词条" : "显示词条";
        try
        {
            var obj = await _httpService.PostAsync<HiddenEntryModel, Result>("api/entries/HiddenEntry", new HiddenEntryModel { Ids = new int[] { model.Id }, IsHidden = isHidden });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }

    public async Task HideItemOutlink(EntryOverviewModel model, bool isHidden)
    {
        string operationName = isHidden ? "隐藏外部链接" : "显示外部链接";
        try
        {
            var obj = await _httpService.PostAsync<HiddenEntryModel, Result>("api/entries/HideEntryOutlink", new HiddenEntryModel { Ids = new int[] { model.Id }, IsHidden = isHidden });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }
    public async Task EditItemCanComment(EntryOverviewModel model, bool canComment)
    {
        string operationName = canComment ? "开启留言板" : "关闭留言板";
        try
        {
            var obj = await _httpService.PostAsync<EditEntryCanCommentModel, Result>("api/comments/EditEntryCanComment", new EditEntryCanCommentModel { Ids = new int[] { model.Id }, CanComment = canComment });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }
}
